import axios from 'axios';
import {delCookie} from "../utils/util";

// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};

// 通用公用方法
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
          token: localStorage.getItem('token'),
          username: localStorage.getItem('username')
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};

// 通用公用方法
export const reqImg = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/octet-stream;charset=utf-8',
      token: localStorage.getItem('token'),
      username: localStorage.getItem('username')
    },
    data: params,
    traditional: true,
    transformRequest: [
      function(data) {
        let ret = ''
        for (let it in data) {
          ret +=
            encodeURIComponent(it) +
            '=' +
            encodeURIComponent(data[it]) +
            '&'
        }
        return ret
      }
    ]
  }).then(res => res.data);
};


// 通用公用方法
export const reqJson = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      token: localStorage.getItem('token'),
      username: localStorage.getItem('username')
    },
    data: params,
    traditional: true,
    transformRequest: [
      function(data) {
        let ret = ''
        for (let it in data) {
          ret +=
            encodeURIComponent(it) +
            '=' +
            encodeURIComponent(data[it]) +
            '&'
        }
        return ret
      }
    ]
  }).then(res => res.data);
};

//弹出提示框方法
const lert=(result)=>{

  if (result === 1) {
    localStorage.removeItem("userInfo")
    window.alert("对不起，您的账号在别处登录");
    window.location.reload(); //监测到路由发生跳转时刷新一次页面
  }else if (result===3) {
    localStorage.removeItem("userInfo")
    window.alert("对不起，您的登录信息已过期，请重新登录");
    window.location.reload(); //监测到路由发生跳转时刷新一次页面
  }

  if (result===0){
    localStorage.removeItem("userInfo");
    window.alert("你已经被踢下线了");
    window.location.reload(); //监测到路由发生跳转时刷新一次页面

  }

}

export {
  loginreq,
  req,
  lert
};
